const cheerio = require('cheerio')
const http = require('http')
const fs = require('fs')
const download = require('./utils/download')
const httpUtil = require('./utils/httpUtil')
const zlib = require('zlib')
const url = require('url')
const createdir = require('./utils/createdir').mkdir
const async = require('async')
let concurrentCount = 0


function test2() {
    downloadItem(`https://www.meitulu.com/item/10518.html`, 'www.meitulu.com/');
    // downloadType(`https://www.meitulu.com/t/wangluohongren/`);
}
test2()

function downloadType(u) {
    httpUtil.getUrl(u)
        .then((buf, res) => {
            let $ = cheerio.load(buf.toString('utf-8')),
                lis = $('ul.img>li'), //get each item
                alist = [],
                host = url.parse(u).host,
                typename = u.substring(u.lastIndexOf('/t/') + 3).trim().replace('/', '') //get type name as dirname=> sitename/typename


            let modelname = $('.listtags_r>h1').text().split('|')[0].split('_')[0]

            for (let i = 0; i < lis.length; i++) {
                let href = lis.eq(i).find('a').attr('href'),
                    p0 = lis.eq(i).find('p').eq(0).text(),
                    p1 = lis.eq(i).find('p').eq(1).text(),
                    p2 = lis.eq(i).find('p').eq(2).text(),
                    tags = lis.eq(i).find('p').eq(3).find('.tags'),
                    taglist = []

                let count = p0.split(/ /)[1] //图片： 34 张(860X1280)=> 34 liyong kongge
                for (let i = 0; i < tags.length; i++) { //just add 2 tag
                    taglist.push(tags.eq(i).text())
                }
                let desc = taglist.join('_')
                let dirname2 = `${host}/${modelname}_${typename}/${count}_${desc}_`

                alist.push({ href: href, dirname: dirname2 })
            }
            for (let i = 0; i < alist.length; i++) {
                let h = alist[i];
                if (i > 0) {
                    break;
                }
                downloadItem(h.href, h.dirname);
            }
        })
        .catch(err => console.log(err))
}

function downloadItem(u, dname) {
    console.log(`current download item ${u}`);
    let i = 1,
        count = 1,
        itemnum = u.split(/item\//)[1].split(/[_.]/)[0], //https://www.meitulu.com/item/4075_2.html=> 4075
        dirname = `${(dname || '')}${itemnum}`,
        threadcount = 0

    function writeUrls(urls) {
        createdir('tmp')
        let path = 'tmp/urls.txt',
            links = urls.map(function (e, i, arr) {
                return e.url
            }),
            dirname = urls[0].dirname,
            data = `${dirname}_${links.length}\r\n\t${links.sort().join('\n\t')}\r\n\r\n`
        fs.appendFile(path, data, function () { })
    }

    function downloadUrls(urls) {

        for (let i = 0; i < urls.length; i++) {
            let url = urls[i]
            setTimeout(function () {
                download.downloadUrl(url.url, url.dirname, url.filename);
            }, i * 100)
        }
        // function fetchUrl(url, callback) {
        //     threadcount++;

        //     setTimeout(() => {
        //         callback(null, url + ' html content');
        //         threadcount--
        //     }, 100)
        // }
        // async.mapLimit(urls, 100, function(url, callback) {
        //     fetchUrl(url, callback)
        // }, function(err, log) {

        // })
    }

    httpUtil.getUrl(u)
        .then((buf) => {
            // fs.writeFileSync(`./tmp/item.html`, buf.toString('utf-8'))
            let $ = cheerio.load(buf.toString()),
                imgs = $('.content img'),
                urls = []


            for (let i = 0; i < imgs.length; i++) {
                let url = imgs.eq(i).attr('src')
                let filename = url.substring(url.lastIndexOf('/') + 1)
                setTimeout(() => {
                    download.downloadUrl(url, dirname, filename);
                }, 100 * i)
                // urls.push({ dirname: `${dirname}`, filename: `${filename}`, url: url })
            }
            // writeUrls(urls);
            // downloadUrls(urls);
            downloadOtherPages();

            function downloadOtherPages() {
                var pages = $('#pages>a')
                for (let i = 0; i < pages.length; i++) {
                    const p = url.parse(u),
                        item = p.protocol + "//" + p.host + pages.eq(i).attr('href')
                    urls = [],
                        httpUtil.getUrl(item)
                            .then(buf => {
                                let $ = cheerio.load(buf.toString())
                                let imgs = $('.content img')
                                for (let i = 0; i < imgs.length; i++) {
                                    let url = imgs.eq(i).attr('src')
                                    let filename = url.substring(url.lastIndexOf('/') + 1)
                                    setTimeout(() => {
                                        download.downloadUrl(url, dirname, filename);
                                    }, 300 * i)
                                    // urls.push({ dirname: `${dirname}`, filename: `${filename}`, url: url })
                                }
                                // writeUrls(urls);
                                // downloadUrls(urls);
                            })
                            .catch(err => {
                                console.log(err)
                            })
                }
            }

        })
        .catch(err => {
            console.log(err)
        })

}



function downloadIndex(url) {
    httpUtil.getUrl(url)
        .then(buf => {

        })
        .catch(err => {
            console.log(err)
        })
}